package com.system.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.system.business.domain.DailyTrain;
import com.system.business.domain.TrainStation;
import com.system.business.req.TrainStationQueryReq;
import com.system.business.req.TrainStationSaveReq;
import com.system.business.resp.DailyTrainQueryResp;
import com.system.business.resp.TrainStationQueryResp;
import com.system.business.service.TrainStationService;
import com.system.business.mapper.TrainStationMapper;
import com.system.common.resp.PageResp;
import com.system.common.utils.SnowUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Admin
* @description 针对表【train_station(火车车站)】的数据库操作Service实现
* @createDate 2024-03-21 15:02:16
*/
@Service
public class TrainStationServiceImpl extends ServiceImpl<TrainStationMapper, TrainStation>
    implements TrainStationService{

    @Autowired
    private TrainStationMapper trainStationMapper;


    @Override
    public PageResp<TrainStationQueryResp>  queryList(TrainStationQueryReq req) {
        Page<TrainStation> pageCondition = new Page<>(req.getPage(), req.getSize());
        QueryWrapper<TrainStation> passengerQueryWrapper = new QueryWrapper<>();
        passengerQueryWrapper.eq("train_code",req.getTrainCode());
        Page<TrainStation> pageData = trainStationMapper.selectPage(pageCondition,passengerQueryWrapper);
        List<TrainStationQueryResp> list = BeanUtil.copyToList(pageData.getRecords(), TrainStationQueryResp.class);

        PageResp<TrainStationQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageData.getTotal());
        pageResp.setList(list);
        return pageResp;
    }

    @Override
    public void save(TrainStationSaveReq req) {
        DateTime now = DateTime.now();

        TrainStation trainStation = BeanUtil.copyProperties(req, TrainStation.class);
        if (ObjectUtil.isNull(trainStation.getId())) {
            trainStation.setId(SnowUtil.getSnowflakeNextId());
            trainStation.setCreateTime(now);
            trainStation.setUpdateTime(now);
            trainStationMapper.insert(trainStation);
        } else {
            trainStation.setUpdateTime(now);
            trainStationMapper.updateById(trainStation);
        }
    }

    @Override
    public List<TrainStation> selectByTrainCode(String trainCode) {
        QueryWrapper<TrainStation> trainStationQueryWrapper = new QueryWrapper<>();
        trainStationQueryWrapper.eq("train_code",trainCode);
        return trainStationMapper.selectList(trainStationQueryWrapper);
    }
}




